package com.cbw.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cbw.blog.entity.Article;
import com.cbw.blog.entity.Comment;
import com.cbw.blog.service.IArticleService;
import com.cbw.blog.service.ICommentService;
import com.cbw.blog.service.ISiteService;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static jdk.nashorn.internal.objects.NativeMath.log;

@Slf4j
@Controller
public class IndexController {

    @Autowired
    private IArticleService articleService;
    @Autowired
    private ICommentService commentService;

    @Autowired
    private ISiteService siteService;

    @GetMapping("/")
    public String index(HttpServletRequest request){
//        return "client/index";
        return this.index(request,1,5);
    }


//首页分页
    @GetMapping("/page/{current}")
    public String index(HttpServletRequest request,
                        @PathVariable("current")Integer current,
                        @RequestParam(value = "count",defaultValue = "5") Integer count){
        Page<Article> articlePage = new Page<>(current,count);
        articleService.page(articlePage, new QueryWrapper<Article>().orderByDesc("id"));
        List<Article> heatArticles = articleService.getHeatArticles();
        request.setAttribute("articles",articlePage);
        request.setAttribute("articleList",heatArticles);
        IndexController.log.info("分页获取文章信息：页码"+current+",条数"+articlePage.getRecords().size());
        return "client/index";
    }
    //文章详细查询
    @GetMapping("/article/{id}")
    public String getArticleById(@PathVariable("id") Integer id,
                                 HttpServletRequest request){
        Article article = articleService.selectArticleWithId(id);
        if (article!=null){
            getArticleComments(request,article);
            //更新文章点击量
            siteService.updateStatistics(article);
            System.out.println(article);
            request.setAttribute("article",article);
            return "client/articleDetails";
        }else{
            log("查询文章相亲结果为空，查询文章ID:{}",id);
            return "comm/error_404";
        }
    }

    private void getArticleComments(HttpServletRequest request, Article article) {
        if (article.getAllowComment()){
            String cp = request.getParameter("cp");
           cp= StringUtils.isBlank(cp)?"1":cp;
           request.setAttribute("cp",cp);


            Page<Comment> commentPage = new Page<>(Integer.parseInt(cp), 3);
            Page<Comment> comments = commentService.page(commentPage, new QueryWrapper<Comment>()
                    .eq("article_id", article.getId())
                    .orderByDesc("id"));

            request.setAttribute("cp",cp);
            request.setAttribute("comments",comments);
        }
    }

}
